#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
//#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#endif

#include "DNA_listBase.h"
#include "cameraReconstruction.h"

#define MAIN_WIN "Single image camera reconstruction for blender"




IplImage* imagen;
IplImage* screenBuffer;
vec2i init_point;
vec2i actual_point;
int drawing_line;
int axis;


CvScalar xAxisColor;
CvScalar yAxisColor;
CvScalar zAxisColor;


void draw(){
	int d=imagen->height;
	cvReleaseImage(&screenBuffer);
	//Get clean copy of image
	screenBuffer=cvCloneImage(imagen);
	//Draw a X axis
	Link *item=(Link*)xLines.first;
	while(item){
		MyLine* l=(MyLine*)item;
		cvLine(screenBuffer, cvPoint(l->init.x,d-l->init.y), cvPoint(l->end.x,d-l->end.y), xAxisColor ,1,CV_AA,0);
		item=item->next;
	}	
	//Draw a X axis
	item=(Link*)yLines.first;
	while(item){
		MyLine* l=(MyLine*)item;
		cvLine(screenBuffer, cvPoint(l->init.x,d-l->init.y), cvPoint(l->end.x,d-l->end.y), yAxisColor ,1,CV_AA,0);
		item=item->next;
	}	
	//Draw a X axis
	item=(Link*)zLines.first;
	while(item){
		MyLine* l=(MyLine*)item;
		cvLine(screenBuffer, cvPoint(l->init.x,d-l->init.y), cvPoint(l->end.x,d-l->end.y), zAxisColor ,1,CV_AA,0);
		item=item->next;
	}	
	//cvCircle(screenBuffer, cvPoint(x,y), r, CV_RGB(0,0,0), 1, CV_AA, 0);
	if(drawing_line){
		CvScalar color= xAxisColor;
		if(axis==Y)
			color=yAxisColor;
		if(axis==Z)
			color=zAxisColor;
		cvLine(screenBuffer, cvPoint(init_point.x,d-init_point.y), cvPoint(actual_point.x,d-actual_point.y), color ,1,CV_AA,0);
	}
}


/*************************
* Mouse CallBack
* event: 
*	#define CV_EVENT_MOUSEMOVE      0
*	#define CV_EVENT_LBUTTONDOWN    1
*	#define CV_EVENT_RBUTTONDOWN    2
*	#define CV_EVENT_MBUTTONDOWN    3
*	#define CV_EVENT_LBUTTONUP      4
*	#define CV_EVENT_RBUTTONUP      5
*	#define CV_EVENT_MBUTTONUP      6
*	#define CV_EVENT_LBUTTONDBLCLK  7
*	#define CV_EVENT_RBUTTONDBLCLK  8
*	#define CV_EVENT_MBUTTONDBLCLK  9
*
* x, y: mouse position
*
* flag:
*	#define CV_EVENT_FLAG_LBUTTON   1
*	#define CV_EVENT_FLAG_RBUTTON   2
*	#define CV_EVENT_FLAG_MBUTTON   4
*	#define CV_EVENT_FLAG_CTRLKEY   8
*	#define CV_EVENT_FLAG_SHIFTKEY  16
*	#define CV_EVENT_FLAG_ALTKEY    32
*
**************************/

void on_mouse( int event, int x, int y, int flags, void* param )
{
	actual_point.x=x;
	actual_point.y= imagen->height - y;
	//Select mouse Event
	if(event==CV_EVENT_LBUTTONUP)
	{
		if(drawing_line){
			addLine(axis,init_point, actual_point);	
			drawing_line=0;			
		}else{
			drawing_line=1;
			init_point.x=x;
			init_point.y=imagen->height-y;
		}
	}
}



int main( int argc, char** argv )
{
	char* filename;
	if(argc >=2 )
		filename=argv[1];
	else{
		printf("Please specify one image\n");
		return 0;
	}

	imagen=cvLoadImage(filename,1);

    printf( "Mouse Event and drawing sample\n"
		"Hot keys: \n"
	"\tr - reset image\n"
	"\tx - X Defining X Line\n"
	"\ty - Y Defining Y Line\n"
	"\tz - Z Defining Z Line\n"
	"\tc - Calculate parameters\n"
        "\tESC - quit the program\n"
        "\tDavid Millan Escriva | Damiles\n");
	drawing_line=0;
	xAxisColor=cvScalar(255,0,0,0);
	yAxisColor=cvScalar(0,255,0,0);
	zAxisColor=cvScalar(0,0,255,0);
	
	//Image we show user with cursor and other artefacts we need
	screenBuffer=cvCloneImage(imagen);
	
	//Create window
    	cvNamedWindow( MAIN_WIN, CV_WINDOW_AUTOSIZE );
	//Create track Bar

	//Create mouse CallBack
	cvSetMouseCallback(MAIN_WIN,&on_mouse, 0 );

	//Main Loop
    for(;;)
    {
	int c;
	draw();
        cvShowImage( MAIN_WIN, screenBuffer );
        c = cvWaitKey(10);
        if( (char) c == 27 )
            drawing_line=0;
	if( (char) c== 'q' )
		break;
	if( (char) c== 'x' ){
		axis=X;
	}
	if( (char)c== 'y') {
		axis=Y;
	}
	if( (char)c== 'z'){
		axis=Z;
	}
	if( (char)c== 'c'){
		calculateCameraParams();
	}		
    }

    cvDestroyWindow(MAIN_WIN);

    return 0;
}

#ifdef _EiC
main(1,"main.c");
#endif
